package ${commonPackage};

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

import ${commonPackage}.param.BaseParam;

/**
 * 基础DAO
 * 
 * @author ${author}
 * @version ${currentDate} ${author}
 * @param <T> 查询返回数据对象
 */
public class BaseDao<T> extends DynamicSqlSessionDaoSupport {

    @Override
    @Autowired
    public void setSqlSessionTemplate(SqlSession sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    /**
     * 分页查询数据信息
     * 
     * @param statment sql名称
     * @param page 分页信息对象
     * @param paramter 查询参数信息
     * @return 分页查询列表
     */
    public List<T> queryList(String statment,BaseParam paramter) {

        Integer totalRows = this.getSqlSession().selectOne(statment + "_count", paramter);

        // 设置总记录数
        page.setTotalCount(totalRows);
        if (totalRows == 0) {
            page.setTotalPage(0);
            page.setTotalCount(0);
            return new ArrayList<T>();
        } else {
            page.setTotalPage(totalRows + 1 / page.getNumPerPage());
            paramter.setStartNum((page.getPageNum() - 1) * page.getNumPerPage());
            paramter.setQueryNum(page.getNumPerPage());

            // 设置排序
            if (!StringUtils.isEmpty(page.getOrderField())) {
                paramter.setOrderField(page.getOrderField());
                if (page.getOrderDirection() == null) {
                    paramter.setOrderDirection(Page.ORDER_DIRECTION_ASC);
                } else {
                    paramter.setOrderDirection(page.getOrderDirection());
                }
            }
            return this.getSqlSession().selectList(statment, paramter);
        }
    }
}